// 表单校验规则由 schema2code 生成，不建议直接修改校验规则，而建议通过 schema2code 生成, 详情: https://uniapp.dcloud.net.cn/uniCloud/schema


const validator = {
  "customer_id": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      }
    ],
    "title": "客户",
    "label": "客户"
  },
  "phone": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      },
      {
        "pattern": "^1[3-9]\\d{9}$"
      }
    ],
    "title": "手机号",
    "label": "手机号"
  },
  "mainContactPhone": {
    "rules": [
      {
        "format": "string"
      },
      {
        "pattern": "^1[3-9]\\d{9}$"
      }
    ],
    "title": "主要联系人电话",
    "label": "主要联系人电话"
  },
  "loanPeriod": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "贷款期限(月)",
    "label": "贷款期限(月)"
  },
  "customerSource": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "客户来源",
    "label": "客户来源"
  },
  "idCard": {
    "rules": [
      {
        "format": "string"
      },
      {
        "pattern": "(^\\d{15}$)|(^\\d{18}$)|(^\\d{17}(\\d|X|x)$)"
      }
    ],
    "title": "身份证号",
    "label": "身份证号"
  },
  "maritalStatus": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "婚姻状态",
    "label": "婚姻状态"
  },
  "remark": {
    "rules": [
      {
        "format": "string"
      },
      {
        "maxLength": 500
      }
    ],
    "title": "备注",
    "label": "备注"
  },
  "intendedProduct": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "意向产品",
    "label": "意向产品"
  },
  "requiredAmount": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "需求金额(万)",
    "label": "需求金额(万)"
  },
  "city": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "所在城市",
    "label": "所在城市"
  },
  "currentCompanyTenure": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "当前单位工龄(月)",
    "label": "当前单位工龄(月)"
  },
  "salaryPaymentMethod": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "工资发放形式",
    "label": "工资发放形式"
  },
  "monthlyIncome": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "月收入(元)",
    "label": "月收入(元)"
  },
  "occupation": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "职业身份",
    "label": "职业身份"
  },
  "loanPurpose": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "贷款用途",
    "label": "贷款用途"
  },
  "socialSecurity": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "社保",
    "label": "社保"
  },
  "housingFund": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "公积金",
    "label": "公积金"
  },
  "carStatus": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "车产情况",
    "label": "车产情况"
  },
  "houseStatus": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "房产情况",
    "label": "房产情况"
  },
  "zhimaScore": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "芝麻分",
    "label": "芝麻分"
  },
  "creditCardStatus": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "信用卡情况",
    "label": "信用卡情况"
  },
  "insurancePolicy": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "保险保单",
    "label": "保险保单"
  },
  "debtStatus": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "负债情况",
    "label": "负债情况"
  },
  "spouseName": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "配偶姓名",
    "label": "配偶姓名"
  },
  "spouseAge": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "配偶年龄",
    "label": "配偶年龄"
  },
  "spouseOccupation": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "配偶职业",
    "label": "配偶职业"
  },
  "isSpouseInformed": {
    "rules": [
      {
        "format": "boolean"
      }
    ],
    "title": "配偶是否知情",
    "label": "配偶是否知情"
  },
  "isSpouseResponsible": {
    "rules": [
      {
        "format": "boolean"
      }
    ],
    "title": "配偶是否负责",
    "label": "配偶是否负责"
  },
  "creditReport": {
    "rules": [
      {
        "format": "array"
      }
    ],
    "title": "征信上传",
    "label": "征信上传"
  },
  "housePropertyDocs": {
    "rules": [
      {
        "format": "array"
      }
    ],
    "title": "房产资料上传",
    "label": "房产资料上传"
  },
  "idCardDocs": {
    "rules": [
      {
        "format": "array"
      }
    ],
    "title": "身份证上传",
    "label": "身份证上传"
  },
  "otherDocs": {
    "rules": [
      {
        "format": "array"
      }
    ],
    "title": "其它上传",
    "label": "其它上传"
  },
  "createBy": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      }
    ],
    "title": "创建人",
    "label": "创建人"
  },
  "createTime": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "timestamp"
      }
    ],
    "title": "创建时间",
    "defaultValue": {
      "$env": "now"
    },
    "label": "创建时间"
  },
  "belongDept": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "归属部门",
    "label": "归属部门"
  },
  "belongTo": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "归属人",
    "label": "归属人"
  },
  "lastFollowTime": {
    "rules": [
      {
        "format": "timestamp"
      }
    ],
    "title": "最近跟进时间",
    "label": "最近跟进时间"
  },
  "lastFollowBy": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "最近跟进人",
    "label": "最近跟进人"
  },
  "totalFollowCount": {
    "rules": [
      {
        "format": "int"
      }
    ],
    "title": "所有跟进次数",
    "defaultValue": 0,
    "label": "所有跟进次数"
  },
  "todayFollowCount": {
    "rules": [
      {
        "format": "int"
      }
    ],
    "title": "当日跟进次数",
    "defaultValue": 0,
    "label": "当日跟进次数"
  },
  "handlingStatus": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "受理情况",
    "label": "受理情况"
  },
  "lastUpdateBy": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "最近修改人",
    "label": "最近修改人"
  }
}

const enumConverter = {
 
}

function filterToWhere(filter, command) {
  let where = {}
  for (let field in filter) {
    let { type, value } = filter[field]
    switch (type) {
      case "search":
        if (typeof value === 'string' && value.length) {
          where[field] = new RegExp(value)
        }
        break;
      case "select":
        if (value.length) {
          let selectValue = []
          for (let s of value) {
            selectValue.push(command.eq(s))
          }
          where[field] = command.or(selectValue)
        }
        break;
      case "range":
        if (value.length) {
          let gt = value[0]
          let lt = value[1]
          where[field] = command.and([command.gte(gt), command.lte(lt)])
        }
        break;
      case "date":
        if (value.length) {
          let [s, e] = value
          let startDate = new Date(s)
          let endDate = new Date(e)
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
      case "timestamp":
        if (value.length) {
          let [startDate, endDate] = value
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
    }
  }
  return where
}

export { validator, enumConverter, filterToWhere }
